package com.briup.web.servlet;

import com.briup.bean.Customer;
import com.briup.bean.ShopCar;
import com.briup.service.IShopCarService;
import com.briup.service.impl.ShopCarServiceIml;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ShopCarAddServlet", value = "/shopCarAdd")
public class ShopCarAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        IShopCarService shopCarService = new ShopCarServiceIml();

        Integer num = Integer.valueOf(request.getParameter("num"));
        Integer bookId = Integer.valueOf(request.getParameter("bookId"));

        //根据session获取当前用户信息
        HttpSession session = request.getSession();
        Customer customer = (Customer) session.getAttribute("customer");

        //customer.getId()通过这个方法获取到用户的id
        Integer customerId = customer.getId();
        //把num、bookId、customerId封装成一个ShopCat类型
        ShopCar shopCar = new ShopCar(num, bookId, customerId);

        //调用service层的saveShopCat方法
        shopCarService.saveShopCar(shopCar);


        //根据用户id 查询出当前用户的所有购物车记录
        List<ShopCar> shopCarList = shopCarService.findShopCarsByCustomer(customerId);
        //把结果集放到session里
        session.setAttribute("shopCarList", shopCarList);
        //跳转到shopCat.jsp
        response.sendRedirect("/shopCar.jsp");


    }

}
